from tkinter import *
import tkinter as tk
import datetime
import os
from pathlib import Path

window = tk.Tk()
window.title("Учёт воды")

my_file = Path("data.txt")
if(my_file.is_file()):
    print("File Exist, data correct")
else:
    data = [0, 0, 0, 0, 0, 0, 0]
    strdata = [str(data[0]), str(data[1]), str(data[2]), str(data[3]), str(data[4]), str(data[5]), str(data[6])]
    with open('data.txt', 'w') as fw:
        fw.write(strdata[0] + '\n' + strdata[1] + '\n' + strdata[2] + '\n' + strdata[3] + '\n' + strdata[4] + '\n' + strdata[5] + '\n' + strdata[6])

my_file = Path("deleted")
if(my_file.is_file() and datetime.datetime.today().isoweekday() == 1):
    print("Today data is nulled")
elif(datetime.datetime.today().isoweekday() == 1):
    os.remove("data.txt")
    data = [0, 0, 0, 0, 0, 0, 0]
    strdata = [str(data[0]), str(data[1]), str(data[2]), str(data[3]), str(data[4]), str(data[5]), str(data[6])]
    with open('data.txt', 'w') as fw:
        fw.write(strdata[0] + '\n' + strdata[1] + '\n' + strdata[2] + '\n' + strdata[3] + '\n' + strdata[4] + '\n' + strdata[5] + '\n' + strdata[6])
    with open('deleted', 'w') as fw:
        fw.write("deleted")    
elif(datetime.datetime.today().isoweekday() == 2 and my_file.is_file()):
    os.remove("deleted")

with open("data.txt", 'r') as fr:
    data = fr.read().splitlines()

def stats():
    with open("data.txt", 'r') as fr:
        data = fr.read().splitlines()

    lbl_pon = Label(text=data[0])
    lbl_vto = Label(text=data[1])
    lbl_sre = Label(text=data[2])
    lbl_che = Label(text=data[3])
    lbl_pya = Label(text=data[4])
    lbl_sub = Label(text=data[5])
    lbl_vos = Label(text=data[6])

    lbl_pon.grid(column=1,row=2)
    lbl_vto.grid(column=1,row=3)
    lbl_sre.grid(column=1,row=4)
    lbl_che.grid(column=1,row=5)
    lbl_pya.grid(column=1,row=6)
    lbl_sub.grid(column=1,row=7)
    lbl_vos.grid(column=1,row=8)

def add_200():
    today = int(datetime.datetime.today().isoweekday()) - 1
    data = [0, 0, 0, 0, 0, 0, 0]
    with open("data.txt", 'r') as fr:
        data = fr.read().splitlines()
    data[0] = int(data[0])
    data[1] = int(data[1])
    data[2] = int(data[2])
    data[3] = int(data[3])
    data[4] = int(data[4])
    data[5] = int(data[5])
    data[6] = int(data[6])
    data[today] = data[today] + 200
    strdata = [str(data[0]), str(data[1]), str(data[2]), str(data[3]), str(data[4]), str(data[5]), str(data[6])]
    with open('data.txt', 'w') as fw:
        fw.write(strdata[0] + '\n' + strdata[1] + '\n' + strdata[2] + '\n' + strdata[3] + '\n' + strdata[4] + '\n' + strdata[5] + '\n' + strdata[6])
def add_250():
    today = int(datetime.datetime.today().isoweekday()) - 1
    data = [0, 0, 0, 0, 0, 0, 0]
    with open("data.txt", 'r') as fr:
        data = fr.read().splitlines()
    data[0] = int(data[0])
    data[1] = int(data[1])
    data[2] = int(data[2])
    data[3] = int(data[3])
    data[4] = int(data[4])
    data[5] = int(data[5])
    data[6] = int(data[6])
    data[today] = data[today] + 250
    strdata = [str(data[0]), str(data[1]), str(data[2]), str(data[3]), str(data[4]), str(data[5]), str(data[6])]
    with open('data.txt', 'w') as fw:
        fw.write(strdata[0] + '\n' + strdata[1] + '\n' + strdata[2] + '\n' + strdata[3] + '\n' + strdata[4] + '\n' + strdata[5] + '\n' + strdata[6])
def add_400():
    today = int(datetime.datetime.today().isoweekday()) - 1
    data = [0, 0, 0, 0, 0, 0, 0]
    with open("data.txt", 'r') as fr:
        data = fr.read().splitlines()
    data[0] = int(data[0])
    data[1] = int(data[1])
    data[2] = int(data[2])
    data[3] = int(data[3])
    data[4] = int(data[4])
    data[5] = int(data[5])
    data[6] = int(data[6])
    data[today] = data[today] + 400
    strdata = [str(data[0]), str(data[1]), str(data[2]), str(data[3]), str(data[4]), str(data[5]), str(data[6])]
    with open('data.txt', 'w') as fw:
        fw.write(strdata[0] + '\n' + strdata[1] + '\n' + strdata[2] + '\n' + strdata[3] + '\n' + strdata[4] + '\n' + strdata[5] + '\n' + strdata[6])
def add_500():
    today = int(datetime.datetime.today().isoweekday()) - 1
    data = [0, 0, 0, 0, 0, 0, 0]
    with open("data.txt", 'r') as fr:
        data = fr.read().splitlines()
    data[0] = int(data[0])
    data[1] = int(data[1])
    data[2] = int(data[2])
    data[3] = int(data[3])
    data[4] = int(data[4])
    data[5] = int(data[5])
    data[6] = int(data[6])
    data[today] = data[today] + 500
    strdata = [str(data[0]), str(data[1]), str(data[2]), str(data[3]), str(data[4]), str(data[5]), str(data[6])]
    with open('data.txt', 'w') as fw:
        fw.write(strdata[0] + '\n' + strdata[1] + '\n' + strdata[2] + '\n' + strdata[3] + '\n' + strdata[4] + '\n' + strdata[5] + '\n' + strdata[6])

def about():
    about = tk.Toplevel(window)
    about.title("О программе")

    lbl_prog_name = Label(about,text="Название программы: PyWaterControl")
    lbl_author_name = Label(about,text="Автор: ReiCode")

    lbl_prog_name.grid(column=0, row=0)
    lbl_author_name.grid(column=0,row=1)

    about.mainloop()

def rm_data():
    os.remove("data.txt")
    data = [0, 0, 0, 0, 0, 0, 0]
    strdata = [str(data[0]), str(data[1]), str(data[2]), str(data[3]), str(data[4]), str(data[5]), str(data[6])]
    with open('data.txt', 'w') as fw:
        fw.write(strdata[0] + '\n' + strdata[1] + '\n' + strdata[2] + '\n' + strdata[3] + '\n' + strdata[4] + '\n' + strdata[5] + '\n' + strdata[6])

def sync():

    def exp_data():

        def exp():
            with open("data.txt", 'r') as fr:
                strdata = fr.read().splitlines()
            filename = txt_expdata.get()
            with open(filename, 'w') as fw:
                fw.write(strdata[0] + '\n' + strdata[1] + '\n' + strdata[2] + '\n' + strdata[3] + '\n' + strdata[4] + '\n' + strdata[5] + '\n' + strdata[6])

            btn_rmdata = Button(text="Обнулить данные", width=25, height=1, command=rm_data)
            btn_expdata = Button(text="Экспортировать данные", width=25, height=1, command=exp_data)
            btn_syncabout = Button(text="О программе", width=25, height=1, command=about)

            btn_rmdata.grid(column=4,row=6)
            btn_expdata.grid(column=4,row=7)
            btn_syncabout.grid(column=4,row=8)

        lbl_expdata = Label(text="Введите имя файла:")
        txt_expdata = Entry(width=25)
        btn_expdata = Button(text="Экспортировать", width=25, height=1, command=exp)
        
        lbl_expdata.grid(column=4,row=6)
        txt_expdata.grid(column=4,row=7)
        btn_expdata.grid(column=4,row=8)


    btn_rmdata = Button(text="Обнулить данные", width=25, height=1, command=rm_data)
    btn_expdata = Button(text="Экспортировать данные", width=25, height=1, command=exp_data)
    btn_syncabout = Button(text="О программе", width=25, height=1, command=about)

    btn_rmdata.grid(column=4,row=6)
    btn_expdata.grid(column=4,row=7)
    btn_syncabout.grid(column=4,row=8)

btn_add_200ml = Button(text="Добавить стакан 200МЛ", width=25, height=1, command=add_200)
btn_add_250ml = Button(text="Добавить стакан 250МЛ", width=25, height=1, command=add_250)
btn_add_400ml = Button(text="Добавить стакан 400МЛ", width=25, height=1, command=add_400)
btn_add_500ml = Button(text="Добавить стакан 500МЛ", width=25, height=1, command=add_500)
btn_stat = Button(text="Обновить статистику", width=50, height=2, command=stats)
btn_sync = Button(text="Синхронизация", width=25, height=1, command=sync)
btn_about = Button(text="О программе", width=25, height=1, command=about)
btn_exit = Button(text="Выйти", width=25, height=2, command=window.destroy)
lbl_pon_nam = Label( text="Понедельник: ")
lbl_pon = Label( text=data[0])
lbl_vto_nam = Label( text="Вторник: ")
lbl_vto = Label( text=data[1])
lbl_sre_nam = Label( text="Среда: ")
lbl_sre = Label( text=data[2])
lbl_che_nam = Label( text="Четверг: ")
lbl_che = Label( text=data[3])
lbl_pya_nam = Label( text="Пятница: ")
lbl_pya = Label( text=data[4])
lbl_sub_nam = Label( text="Суббота: ")
lbl_sub = Label( text=data[5])
lbl_vos_nam = Label( text="Воскресенье: ")
lbl_vos = Label( text=data[6])

lbl_pon_nam.grid(column=0,row=2)
lbl_vto_nam.grid(column=0,row=3)
lbl_sre_nam.grid(column=0,row=4)
lbl_che_nam.grid(column=0,row=5)
lbl_pya_nam.grid(column=0,row=6)
lbl_sub_nam.grid(column=0,row=7)
lbl_vos_nam.grid(column=0,row=8)
lbl_pon.grid(column=1,row=2)
lbl_vto.grid(column=1,row=3)
lbl_sre.grid(column=1,row=4)
lbl_che.grid(column=1,row=5)
lbl_pya.grid(column=1,row=6)
lbl_sub.grid(column=1,row=7)
lbl_vos.grid(column=1,row=8)
btn_stat.grid(columnspan=2,row=0)
btn_sync.grid(column=4,row=6)
btn_about.grid(column=4,row=7)
btn_exit.grid(column=4,row=0)
btn_add_200ml.grid(column=4, row=2)
btn_add_250ml.grid(column=4, row=3)
btn_add_400ml.grid(column=4, row=4)
btn_add_500ml.grid(column=4, row=5)

window.mainloop()